<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\HealthRecords;
use Illuminate\Http\Request;
use DB;

/**
 * Class HealthRecordsController
 * @package App\Http\Controllers
 */
class HealthRecordsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 健康档案列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function healthRecordsList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::HealthRecordsList());
        }

        return view('healthRecords.healthRecordsList');
    }

    /**
     * 健康档案
     * @param HealthRecords $healthRecords
     * @return $this
     */
    public function healthRecordsInfo(HealthRecords $healthRecords)
    {
        if ($healthRecords->status === HealthRecords::STATUS_NEW && $healthRecords->foll_id) {
            $new = DB::table('foll_health_records')->where('id', $healthRecords->foll_id)->first();
        } else {
            $new = NULL;
        }
        return view('healthRecords.healthRecordsInfo')->with(compact('healthRecords', 'new'));
    }

    /**
     * 通过审核
     * @param HealthRecords $healthRecords
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOn(HealthRecords $healthRecords)
    {
        return response()->json(HealthRecords::StatusOn($healthRecords));
    }


    /**
     * 不通过审核
     * @param HealthRecords $healthRecords
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function statusOff(HealthRecords $healthRecords)
    {
        return response()->json(HealthRecords::StatusOff($healthRecords));
    }

    /**
     * 选填通过
     * @param HealthRecords $healthRecords
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function newStatusOn(HealthRecords $healthRecords)
    {
        return response()->json(HealthRecords::NewStatusOn($healthRecords));
    }


    /**
     * 选填审核不通过
     * @param HealthRecords $healthRecords
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function newstatusOff(HealthRecords $healthRecords)
    {
        return response()->json(HealthRecords::NewStatusOff($healthRecords));
    }
}
